﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ERP.ERPSystem.RBAC.Dto;
using ERP.ERPSystem.RBAC;
using ERP.Team.RBAC;

namespace ERP.Web.Controllers.RBAC
{
    public class RoleController : Controller
    {
        private readonly IRoleServices _roleServices;

        public RoleController(IRoleServices _oleServices)
        {
            _roleServices = _oleServices;
        }

        /// <summary>
        /// 角色
        /// </summary>
        /// <returns></returns>
        public IActionResult Roles()
        {
            return View();
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roleDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddRole(RoleDto roleDto)
        {
            var i = await _roleServices.AddRole(roleDto);
            return i;
        }

        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        public IActionResult GetRole()
        {
            return View();
        }

        /// <summary>
        /// 编辑角色视图
        /// </summary>
        /// <returns></returns>
        public IActionResult EditRole()
        {
            return View();
        }

        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<RoleModel>> GetRoles()
        {
            var i = await _roleServices.GetRole();
            return i;
        }

        /// <summary>
        /// 根据ID获取角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<RoleDto> GetRoleById(int id)
        {
            var role = await _roleServices.GetRoleById(id);
            return role;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task DeleteRole(int id)
        {
            await _roleServices.DeleteRole(id);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="roleDto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<int> UpdateRole([FromBody] RoleDto roleDto)
        {
            var i = await _roleServices.UpdateRole(roleDto);
            return i;
        }
        /// <summary>
        /// 编辑角色视图
        /// </summary>
        /// <returns></returns>
        public IActionResult RPIndex()
        {
            return View();
        }
        /// <summary>
        /// 角色分配权限
        /// </summary>
        /// <param name="rolePermissionDTO"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<int> RolePermission(RolePermissionDTO rolePermissionDTO)
        {
            var i = await _roleServices.RolePermission(rolePermissionDTO);
            return i;
        }
        /// <summary>
        /// 反填角色权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<int>> FantianRolePermission(int id)
        {
            var i = await _roleServices.FantianRolePermission(id);
            return i ?? new List<int>();
        }

    }
}
